<template>
    <div class="sidebar">
      <ul>
        <li :class="{ active: currentPath === '/home' }" @click="navigateToHome">
            <i class="iconfont icon-shouye"></i>
            <span>首页</span>
        </li>
        <li :class="{ active: currentPath === '/product' }" @click="navigateToProduct">
            <i class="iconfont icon-suoyouchanpin"></i>
            <span>所有产品</span>
        </li>
        <li :class="{ active: currentPath === '/user' }" @click="navigateToUser">
            <i class="iconfont icon-wodezhanghu"></i>
            <span>我的账户</span>
        </li>
      </ul>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  
  const router = useRouter();
  const route = useRoute();
  const currentPath = ref(route.path);
  
  const navigateToHome = () => {
      router.push('/home');
  }
  
  const navigateToProduct = () => {
      router.push('/product');
  }
  
  const navigateToUser = () => {
      router.push('/user');
  }
  
  // 监听路由变化，更新当前路径状态
  onMounted(() => {
      currentPath.value = route.path;
      router.afterEach((to, from) => {
          currentPath.value = to.path;
      });
  });
  </script>
  
  <style lang="less" scoped>
  @primary:#f5f6fb;
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    margin-top: 80px;
    width: 300px;
    height: 100vh;
    background-color: @primary;
    padding: 20px;
    cursor: pointer;
  
    ul {
      list-style: none;
      padding: 0;
  
      li {
        margin-bottom: 10px;
        font-size: 20px;
        height: 50px;
        line-height: 50px;
        .iconfont {
            font-size: 24px;
            padding: 0 30px;
        }
  
        &:hover, &.active {
            background-color: #f0f0f0;
            color: #333;
        }
      }
    }
  }
  </style>
  